<template>
  <div class="table">
    <div class="crumbs">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item>
          <i class="el-icon-lx-friendadd"></i>广告维护
        </el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="container">
      <div class="handle-box">
        <el-button type="primary" @click="ALL" class="lkx_r20">全部</el-button>
        <el-input
          v-model.trim="select_word"
          placeholder="输入广告ID和广告名称"
          class="handle-input mr10"
          @keyup.enter.native="search_key(select_word)"
        ></el-input>
        <el-button type="primary" icon="search" @click="search_key(select_word)">搜索</el-button>
        <span class="zh_status">账号状态选择:</span>
        <el-select
          v-model="status_value"
          placeholder="请选择"
          class="status_select"
          @change="select_status"
        >
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
        <el-button type="primary" icon="search" @click="select_status">查看</el-button>
      </div>
      <el-table
        border
        ref="singleTable"
        :data="tableData"
        highlight-current-row
        style="width: 100%"
      >
        <el-table-column type="index" width="50" label="序号" align="center"></el-table-column>
        <el-table-column prop="advert_name" label="广告名称" align="center"></el-table-column>
        <el-table-column prop="advert_id" label="广告ID" align="center"></el-table-column>
        <el-table-column prop="status" label="广告状态" align="center"></el-table-column>
        <el-table-column label="申请时间" align="center">
          <template slot-scope="scope">{{ scope.row.apply_time }}</template>
        </el-table-column>
        <el-table-column prop="user_name" label="所属账号" align="center"></el-table-column>        
        <el-table-column prop="review_admin_name" label="审核人员" align="center"></el-table-column>
        <el-table-column label="操作" width="180" align="center">
          <template slot-scope="scope">
            <el-button
              :disabled="scope.row.TFs"
              :type="scope.row.btColor"
              @click="operation(scope.$index, scope.row)"
            >{{ scope.row.btText }}</el-button>
          </template>
        </el-table-column>
        <el-table-column label="查看详情" width="180" align="center">
          <template slot-scope="scope">
            <el-button
              type="text"
              icon="el-icon-edit"
              @click="handleEdit(scope.$index, scope.row)"
            >查看</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <div class="pagination_div">
        <el-pagination
          background
          layout="prev, pager, next"
          :total="Totals"
          :page-count="pageTotal"
          :page-size="10"
          :current-page.sync="currPage"
          @current-change="handleCurrentChange"
        ></el-pagination>
      </div>
    </div>
  </div>
</template>
<script>
import * as api from "../../http/api.js";

export default {
  data() {
    return {
      //表格数据
      tableData: [],
      options: [
        {
          value: "0",
          label: "全部"
        },
        {
          value: "2",
          label: "已驳回"
        },
        {
          value: "1",
          label: "正常"
        },
        {
          value: "4",
          label: "禁用"
        }
      ],
      status_value: "", //默认状态下拉
      select_word: "", //搜索的关键词
      editVisible: false, //查看详情开关
      Token: sessionStorage.getItem("Token"),
      currPage: 1, //分页页码
      Totals: 1, //总条数 默认为1条，在获取接口列表时在传递过来，改变1
      pageTotal: 2, //总页数
      advert_id: "", //广告id 用来连接详情的参数
      typenumber: "" //操作按钮对应的数字
    };
  },
  methods: {
    getData(statuszt,key) {
      //获得表格数据！
      this.$post(api.advert_list, {
        token: this.Token,
        page: this.currPage,
        advert: key,//关键词
        status: statuszt //状态 1通过 2 驳回 4 禁用
      })
        .then(res => {
          if (res.status == "0") {
            this.tableData = res.data.map((val, index, arr) => {
              let jsons = [];
              jsons.advert_name = val.advert_name;
              jsons.advert_id = val.advert_id;
              jsons.status = val.status;
              jsons.apply_time = val.apply_time;
              jsons.user_name = val.user_name;
              jsons.review_admin_name=val.review_admin_name;
              

              jsons.btText = val.btText; //按钮上的文字
              jsons.btColor = val.btColor; //按钮的颜色
              jsons.typenumber = val.typenumber; // 操作所对应的数字含义！
              jsons.TFs = val.TFs; //判断按钮是否禁用
              return jsons;
            });
            this.tableData.forEach((val, index, arr) => {
              if (val.status == "1") {
                val.status = "正常";
                arr[index].btText = "禁用";
                arr[index].btColor = "danger";
                arr[index].typenumber = 3;
              }
              if (val.status == "2") {
                // 这个比较特殊 如果里面是驳回的 点击按钮就不操作！
                val.status = "审核驳回未通过";
                arr[index].btText = "请查看详情";
                arr[index].TFs = true;
              }
              if (val.status == "4") {
                val.status = "禁用";
                arr[index].btText = "启动";
                arr[index].btColor = "primary";
                arr[index].typenumber = 4;
              }
              
            });
            // 获取分页数据
            this.Totals = res.total;
            this.pageTotal = res.pageTotal;
            this.currPage = res.currPage;
          } else {
            console.log(res.msg);
          }
          // 如果登录过期
          if (res.status == "relogin") {
            this.$message.error(res.msg);
            this.$router.push("/login");
          }
        })
        .catch(error => {
          console.log(error);
        });
    },
    ALL(){
      this.status_value="";
      this.select_word="";
      this.currPage=1;
      this.getData(this.status_value,this.select_word);
    },
    //搜索关键词
    search_key(select_word) {
      // 搜索商品编号及商品名称
      if (select_word == "") {
        this.$message.error(`搜索不能为空`);
        return;
      } else {
        this.currPage=1;
        this.getData(this.status_value,this.select_word)
      }
    },
    select_status() {
      this.currPage=1;
      this.getData(this.status_value,this.select_word)
    },
    operation(index, row) {
      //点击操作按钮！
      this.advert_id = this.tableData[index].advert_id; //广告id
      this.typenumber = this.tableData[index].typenumber; //操作对应的数字
      this.$post(api.advert_check, {
        token: this.Token,
        advert_id: this.advert_id,
        type: this.typenumber
      })
        .then(res => {
          if (res.status == "0") {
            this.$message({
              type: "success",
              message: "修改成功！"
            });
            setTimeout(() => {
              this.getData();
            });
          } else {
            this.$message.error(res.msg);
          }
          // 如果登录过期
          if (res.status == "relogin") {
            this.$message.error(res.msg);
            this.$router.push("/login");
          }
        })
        .catch(error => {
          console.log(error);
        });
    },
    //查看详情页面
    handleEdit(index, row) {
      //点击进去详情
      this.advert_id = this.tableData[index].advert_id;
      this.$router.push({
        path: "/MaintainInfo",
        query: { id: this.advert_id }
      });
    },
    //点击分页
    handleCurrentChange(val) {
      this.currPage = val;
      this.getData(this.status_value,this.select_word)
    }
  },
  created() {
    this.getData();
  }
};
</script>
<style scoped>
.mr10 {
  margin-right: 10px;
}
.handle-input {
  width: 300px;
  display: inline-block;
}
.handle-box {
  padding-bottom: 20px;
}
.zh_status {
  line-height: 32px;
  margin-left: 100px;
}
.status_select {
  margin: 0 10px;
}
.pagination_div {
  margin-top: 20px;
  text-align: right;
}
</style>